package com.sgb.merchant.widget;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.link.entity.merchant.IndustryCategoryEntity;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantPopwindowFilterProjectBinding;
import com.sgb.merchant.view.adapter.ItemTypeAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 王彬臣
 * @date: 2021/9/16
 * @Description: 投资项目更多筛选列表
 */
public class ProjectFilterPopWindow extends BasePopWindow implements View.OnClickListener {

    MerchantPopwindowFilterProjectBinding mBinding;

    ItemTypeAdapter revenueAdapter, landAdapter, stageAdapter;

    List<IndustryCategoryEntity> revenueType,halfRevenue;
    List<IndustryCategoryEntity> landType,halfLand;
    List<IndustryCategoryEntity> stageType,halfStage;
    boolean isRevenueMore,isLandMore,isStageMore;

    Map<String, Object> mMap;

    public void setRevenueType(List<IndustryCategoryEntity> revenueType) {
        this.revenueType = revenueType;
        if(revenueType.size()>9){
            mBinding.revenueMore.setVisibility(View.VISIBLE);
            halfRevenue=revenueType.subList(0,9);
            revenueAdapter.setDatas(halfRevenue);
        }else {
            revenueAdapter.setDatas(revenueType);
        }

    }

    public void setLandType(List<IndustryCategoryEntity> landType) {
        this.landType = landType;
        if(landType.size()>9){
            mBinding.landMore.setVisibility(View.VISIBLE);
            halfLand=landType.subList(0,9);
            landAdapter.setDatas(halfLand);
        }else {
            landAdapter.setDatas(landType);
        }
    }

    public void setStageType(List<IndustryCategoryEntity> stageType) {
        this.stageType = stageType;
        if(stageType.size()>9){
            mBinding.stageMore.setVisibility(View.VISIBLE);
            halfStage=stageType.subList(0,9);
            stageAdapter.setDatas(halfStage);
        }else {
            stageAdapter.setDatas(stageType);
        }
    }

    public ProjectFilterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.merchant_popwindow_filter_project;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mMap = new HashMap<>();
        mBinding = (MerchantPopwindowFilterProjectBinding) holder.getmBinding();
        mBinding.setOnClick(this);

        mBinding.revenueRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mBinding.landRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mBinding.stageTypeList.setLayoutManager(new GridLayoutManager(mContext, 3));

        revenueAdapter = new ItemTypeAdapter(mContext, null);
        revenueAdapter.setItemSelect(true);
        revenueAdapter.setDefItem(0);
        mBinding.revenueRecyclerView.setAdapter(revenueAdapter);
        revenueAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                if(entity.getCategoryName().equals("不限")){
                    mMap.remove("taxDiscountNo");
                }else{
                    mMap.put("taxDiscountNo",Long.valueOf(entity.getCategoryNo()));
                }
                mBinding.choosedRevenue.setText(entity.getCategoryName());

            }
        });

        landAdapter = new ItemTypeAdapter(mContext, null);
        landAdapter.setItemSelect(true);
        landAdapter.setDefItem(0);
        mBinding.landRecyclerView.setAdapter(landAdapter);
        landAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                if(entity.getCategoryName().equals("不限")){
                    mMap.remove("landDiscountNo");
                }else{
                    mMap.put("landDiscountNo",entity.getCategoryNo());
                }
                mBinding.choosedLanded.setText(entity.getCategoryName());
            }
        });


        stageAdapter = new ItemTypeAdapter(mContext, null);
        stageAdapter.setItemSelect(true);
        stageAdapter.setDefItem(0);
        mBinding.stageTypeList.setAdapter(stageAdapter);
        stageAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                if(entity.getCategoryName().equals("不限")){
                    mMap.remove("stageNo");
                }else{
                    mMap.put("stageNo",entity.getCategoryNo());
                }
                mBinding.chooseStageType.setText(entity.getCategoryName());
            }
        });


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.sure_step) {
            if (onResultCallBack != null) {
               onResultCallBack.result(mMap);
            }
            dismiss();
        } else if (id == R.id.reset_step) {
            mMap.clear();
            revenueAdapter.setDefItem(0);
            landAdapter.setDefItem(0);
            stageAdapter.setDefItem(0);
            mBinding.choosedRevenue.setText("不限");
            mBinding.choosedLanded.setText("不限");
            mBinding.chooseStageType.setText("不限");

        } else if (id == R.id.revenue_more) {
            if(isRevenueMore){
                isRevenueMore=false;
                mBinding.revenueMore.setImageResource(R.mipmap.merchant_common_shouqi);
                revenueAdapter.setDatas(halfRevenue);
            }else{
                isRevenueMore=true;
                mBinding.revenueMore.setImageResource(R.mipmap.merchant_common_zhankai);
                revenueAdapter.setDatas(revenueType);
            }

        } else if (id == R.id.land_more) {
            if(isLandMore){
                isLandMore=false;
                mBinding.landMore.setImageResource(R.mipmap.merchant_common_shouqi);
                landAdapter.setDatas(halfLand);
            }else{
                isLandMore=true;
                mBinding.landMore.setImageResource(R.mipmap.merchant_common_zhankai);
                landAdapter.setDatas(landType);
            }
        } else if (id == R.id.stage_more) {
            if(isStageMore){
                isStageMore=false;
                mBinding.stageMore.setImageResource(R.mipmap.merchant_common_shouqi);
                stageAdapter.setDatas(halfStage);
            }else{
                isStageMore=true;
                mBinding.stageMore.setImageResource(R.mipmap.merchant_common_zhankai);
                stageAdapter.setDatas(stageType);
            }
        }

    }

    public void setRest(){
        mBinding.resetStep.callOnClick();
    }
}
